
#include <string.h>
#include <stdio.h>
#include "mhscpu.h"
#include "uart.h"
#include "mhscpu_msr.h"
#include "stdlib.h"
#include "string.h"

void UART_Configuration(void);

char in_char()
{
	while(!(UART_GetLineStatus(UART0) & UART_LINE_STATUS_RX_RECVD)) 
	{
		;
	}
	return UART_ReceiveData(UART0);
}



#ifdef __GNUC__
  /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
     set to 'Yes') calls __io_putchar() */
  #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
  #define GETCHAR_PROTOTYPE int __io_getchar(void)
#else
  #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
  #define GETCHAR_PROTOTYPE int fgetc(FILE *f)
#endif /* __GNUC__ */

PUTCHAR_PROTOTYPE
{
    /* Place your implementation of fputc here */
    /* e.g. write a character to the USART */
    
    if (ch == '\n')
    {
        uart_SendChar('\r');
    }
    uart_SendChar(ch);

    return ch;
}


GETCHAR_PROTOTYPE
{
    
	return uart_RecvChar();
}


void NVIC_Configuration(void)
{
	NVIC_InitTypeDef NVIC_InitStructure;
	
	NVIC_SetPriorityGrouping(NVIC_PriorityGroup_3);
	
	NVIC_InitStructure.NVIC_IRQChannel = MSR_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);
}

void print_msr_buf(uint32_t *p, int len)
{
	while (len--)
	{
		printf("%02X ", (uint8_t)*p++);
	}
	printf("\n");
}

int main()
{

	SYSCTRL_APBPeriphClockCmd(SYSCTRL_APBPeriph_UART0 | \
												 SYSCTRL_APBPeriph_GPIO | SYSCTRL_APBPeriph_TRNG | \
												 SYSCTRL_APBPeriph_BPU, ENABLE);
	SYSCTRL_APBPeriphResetCmd(SYSCTRL_APBPeriph_UART0 | \
												 SYSCTRL_APBPeriph_TRNG | \
												 SYSCTRL_APBPeriph_BPU, ENABLE);
	SYSCTRL_AHBPeriphClockCmd(SYSCTRL_AHBPeriph_CRYPT, ENABLE);
	SYSCTRL_AHBPeriphResetCmd(SYSCTRL_AHBPeriph_CRYPT, ENABLE);

	NVIC_Configuration();

	uart_Config(115200, UART_Parity_No); 
	
	printf("MegaHunt SCPU-L MSR Test Demo V1.0\n");
	
	set_wakeup_status(HARD_WAKEUP_WITHOUT_SLEEP);
	init_dpu();

	sc_sleep();
	
	while (1)
	{
	}
}


#ifdef  USE_FULL_ASSERT

/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t* file, uint32_t line)
{ 
	/* User can add his own implementation to report the file name and line number,
	 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

	/* Infinite loop */
	while (1)
	{
	}
}
#endif






